#include "Rotary.h"
#include "HALL_Sensor.h"
#include "stm32f1xx.h"
#include "main.h" 
#include "tim.h"
#include "Rotary_Cfg.h"


static uint8_t  CurrentSector=1;
static Rotrary_Direction  g_dir=Positive_Rotrary;
static Rotrary_Mode  mode=continuous;
static En_Gate_Status ENGate_Status=disable;

void Set_CurrentDir(Rotrary_Direction val);
Rotrary_Direction Get_CurrentDir(void);

void Set_CurrentSector(uint8_t val);
uint8_t Get_CurrentSector(void);

void Set_CurrentMode(Rotrary_Mode val);
Rotrary_Mode Get_CurrentMode(void);

void Set_DutyCycle(uint16_t DutyCycle);

void Set_ENGate_Status(En_Gate_Status states);
En_Gate_Status Get_ENGate_Status(void);

void Toggle_Engate(void)
{
  En_Gate_Status state;
  state = (Get_ENGate_Status()==enable) ? disable : enable;
  Set_ENGate_Status(state);
}

void Bldc_Init(void)
{
  Set_CurrentDir(Positive_Rotrary);
  Set_CurrentMode(continuous);
  Set_DutyCycle(1000);
  Set_ENGate_Status(enable);
}


static void Ctrl_U_positive(Phase_Ctrl_State status);
static void Ctrl_U_negetive(Phase_Ctrl_State status);
static void Ctrl_V_positive(Phase_Ctrl_State status);
static void Ctrl_V_negetive(Phase_Ctrl_State status);
static void Ctrl_W_positive(Phase_Ctrl_State status);
static void Ctrl_W_negetive(Phase_Ctrl_State status);

void (*Ctrl_Sequence_FuncTable[6])(Phase_Ctrl_State)={
                                                    &Ctrl_U_positive,
                                                    &Ctrl_U_negetive,
                                                    &Ctrl_V_positive,
                                                    &Ctrl_V_negetive,
                                                    &Ctrl_W_positive,
                                                    &Ctrl_W_negetive,
                                                  };


uint8_t Channel_States_Positive[6][6]={
                              /*U+ U- V+ V- W+ W-*/
                              {1,0,0,0,0,1},/*sector=1*/  
                              {1,0,0,1,0,0},/*sector=2*/  
                              {0,0,0,1,1,0},/*sector=3*/  
                              {0,1,0,0,1,0},/*sector=4*/  
                              {0,1,1,0,0,0},/*sector=5*/  
                              {0,0,1,0,0,1},/*sector=6*/  
                          };/*正传*/                          

uint8_t Channel_States_Negetive[6][6]={
                              /*U+ U- V+ V- W+ W-*/
                              {0,1,0,0,1,0},
                              {0,1,1,0,0,0},
                              {0,0,1,0,0,1},                              
                              {1,0,0,0,0,1},
                              {1,0,0,1,0,0},
                              {0,0,0,1,1,0},                                                           
                          };/*反传*/                          

void rotary(uint8_t CurrentSector,uint8_t dir)
{
    CurrentSector-=1;/*扇区是1-6，数组是0-5，偏移一下*/
    int i;
    for(i=0;i<6;i++)
    {
      if(Positive_Rotrary==dir)
      {
        Ctrl_Sequence_FuncTable[i](Channel_States_Positive[CurrentSector][i]);        
      }
      else
      {
        Ctrl_Sequence_FuncTable[i](Channel_States_Negetive[CurrentSector][i]);  
      }
    }
}

static void Ctrl_U_positive(Phase_Ctrl_State status)
{
    if(Open==status)
    {
      HAL_TIM_PWM_Start(&htim1,U_PosPhase_Channel);        
    }
    else
    {
      HAL_TIM_PWM_Stop(&htim1,U_PosPhase_Channel);
    }
}

static void Ctrl_V_positive(Phase_Ctrl_State status)
{
    if(Open==status)
    {
      HAL_TIM_PWM_Start(&htim1,V_PosPhase_Channel);        
    }
    else
    {
      HAL_TIM_PWM_Stop(&htim1,V_PosPhase_Channel);
    }
}

static void Ctrl_W_positive(Phase_Ctrl_State status)
{
    if(Open==status)
    {
      HAL_TIM_PWM_Start(&htim1,W_PosPhase_Channel);        
    }
    else
    {
      HAL_TIM_PWM_Stop(&htim1,W_PosPhase_Channel);
    }
}

static void Ctrl_U_negetive(Phase_Ctrl_State status)
{
  HAL_GPIO_WritePin(U_L_GPIO_Port,U_L_Pin,(GPIO_PinState)status);
}

static void Ctrl_V_negetive(Phase_Ctrl_State status)
{
  HAL_GPIO_WritePin(V_L_GPIO_Port,V_L_Pin,(GPIO_PinState)status);
}

static void Ctrl_W_negetive(Phase_Ctrl_State status)
{
  HAL_GPIO_WritePin(W_L_GPIO_Port,W_L_Pin,(GPIO_PinState)status);
}


void Set_CurrentDir(Rotrary_Direction val)
{
    g_dir = val;
}

Rotrary_Direction Get_CurrentDir(void)
{
    return g_dir;
}

void Set_CurrentSector(uint8_t val)
{
    CurrentSector = val;
}

uint8_t Get_CurrentSector(void)
{
    return Get_HALLMotorSector_Loc();
}

void Set_CurrentMode(Rotrary_Mode val)
{
    mode = val;
}

Rotrary_Mode Get_CurrentMode(void)
{
    return mode;
}

void Set_DutyCycle(uint16_t DutyCycle)
{
  __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_1,DutyCycle);
  __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_2,DutyCycle);
  __HAL_TIM_SET_COMPARE(&htim1,TIM_CHANNEL_3,DutyCycle);
}

void Set_ENGate_Status(En_Gate_Status states)
{
  HAL_GPIO_WritePin(EN_GATE_GPIO_Port,EN_GATE_Pin,states);
  ENGate_Status=states;
}

En_Gate_Status Get_ENGate_Status(void)
{
  return ENGate_Status;
}